import { ArticleModel } from '~/server/models/article.model';

export default defineEventHandler(async (event) => {
	const body = await readBody(event);
	const { id, title, content, cover, categoryId, isTop } = body;
	if (!id) {
		return { code: 0, message: '缺少文章ID' };
	}
	try {
		const updated = await ArticleModel.findByIdAndUpdate(
			id,
			{
				title,
				content,
				cover,
				categoryId,
				isTop,
				updatedAt: new Date().toLocaleString('zh-CN', { hour12: false }),
			},
			{ new: true }
		);
		if (!updated) {
			return { code: 0, message: '未找到对应文章' };
		}
		return { code: 1, message: '更新成功', data: updated };
	} catch (e: any) {
		return { code: 0, message: e.message };
	}
});
